/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.utils;

import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import com.reddys.rad.inventory.service.PurchaseOrderService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author REDDYS
 */
   public class EditingCellForSaleReturn extends TableCell<Object, String> {
        private String columnName ;
        private TextField textField;
 PurchaseOrderDetails purchaseOrderDetails ;
        public EditingCellForSaleReturn() {
        }
       public EditingCellForSaleReturn(String columnName ) {
           this.columnName = columnName;
        }
 
        @Override
        public void startEdit() {
           
                super.startEdit();
                createTextField();
                setText(null);
                setGraphic(textField);
                textField.selectAll();

        }
 
        @Override
        public void cancelEdit() {
            super.cancelEdit();
 
            setText((String) getItem());
            setGraphic(null);
        }
 
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
 
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                if (isEditing()) {
                    if (textField != null) {
                        textField.setText(getString());
                    }
                    setText(null);
                    setGraphic(textField);
                } else {
                    setText(getString());
                    setGraphic(null);
                }
            }
        }
     public Property<String> inputText()
    {
        return textField.textProperty();
    }

        private void createTextField() {
            final ObservableList tableList = (ObservableList) this.getTableRow().getItem();
            textField = new TextField(getString());
            textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
            textField.setOnKeyReleased(new EventHandler<KeyEvent>(){

                @Override
                public void handle(KeyEvent t) {
                    System.out.println("createtext"+textField.getText());
                    if(columnName.equalsIgnoreCase("Returned")){
                        if(textField.getText() !=null)
                         tableList.set(7,new Double(textField.getText()));
                    }
                }
 
            });
            textField.setOnMouseExited(new EventHandler<MouseEvent>(){
         
                @Override
                public void handle(MouseEvent t) {
     System.out.println("createtext"+textField.getText());
                    if(columnName.equalsIgnoreCase("Returned")){
                        if(textField.getText() !=null)
                         tableList.set(7,new Double(textField.getText()));
                    }
                   }
             });

            textField.focusedProperty().addListener(new ChangeListener<Boolean>(){
                @Override
                public void changed(ObservableValue<? extends Boolean> arg0, 
                    Boolean arg1, Boolean arg2) {
                        if (!arg2) {
                            commitEdit(textField.getText());
                        }
                }
            });
             if(columnName.equalsIgnoreCase("ProductCategory")){
                System.out.println("column"+columnName);
                       ObservableList data = FXCollections.observableArrayList();
                       data = PurchaseOrderService.getProductCategoryList();
                       ArrayList list = new ArrayList();
                        for (int i = 0; i < data.size(); i++) {
                            list.add(data.get(i));
                        }
                        autoCompleteListView(list);
                       
                    }else if(columnName.equalsIgnoreCase("ProductID")){
                      ObservableList data = FXCollections.observableArrayList();
                       data = PurchaseOrderService.getProductList();
                       ArrayList list = new ArrayList();
                        for (int i = 0; i < data.size(); i++) {
                            list.add(data.get(i));
                        }
                        autoCompleteListView(list);
                       
                    }
        }
 
        private String getString() {
            return getItem() == null ? "" : getItem().toString();
        }
        
        private void autoCompleteListView(ArrayList listItems){
        final AutoCompleteList<String> list = new AutoCompleteList<String>(textField);
        final List<String> allItems = listItems;

System.out.println("allItems->"+allItems);
        // filtering of list
        list.inputText().addListener(new ChangeListener<String>()
        {
            public void changed(ObservableValue<? extends String> source, String oldValue, String newValue)
            {
                list.getAvailableItems().clear();
                for (String item : allItems)
                {
                    if (item.toLowerCase().contains(newValue))
                    {
                        list.getAvailableItems().add(item);
                    }
                }
                list.getAvailableItemsSelectionModel().selectFirst();
            }
        });

        list.getChosenItems().addListener(new ListChangeListener<String>()
        {
            public void onChanged(ListChangeListener.Change<? extends String> change)
            {
                System.out.println("Your destiny consists of: ");
                for (String chosen : list.getChosenItems())
                {
                    System.out.println(" => " + chosen);
                    textField.setText(chosen);
                }
            }
        });
 
        }
    }